import { type NextRequest, NextResponse } from 'next/server'
import axios from 'axios'
import { API_KEY, API_URL } from '@/config'
const text2audio = (text: string) => {
  return axios
    .request({
      method: 'post',
      maxBodyLength: Infinity,
      url: `${API_URL}/text-to-audio`,
      headers: {
        'Authorization': `Bearer ${API_KEY}`,
        'content-type': 'application/json',
      },
      data: JSON.stringify({
        text,
      }),
      responseType: 'arraybuffer',
    })
}
export async function POST(request: NextRequest) {
  // const {
  //   sessionId,
  //   // user,
  // } = getInfo(request)
  const body = await request.json()
  try {
    // const { data }: any = await client.sendRequest('POST', '/text-to-audio', body)
    const { data } = await text2audio(body.text)
    return new Response(data, {
      headers: {
        'content-type': 'audio/wav',
      },
    })
  }
  catch (error: any) {
    console.error('XZ TTS Error:', error.message)
    return NextResponse.json({
      data: [],
      error: error.message,
    })
  }
}
